<template>
  <div class="">
    <SearchContainer @search="getData" @reset="() => formRef.resetFields()">
      <a-form ref="formRef" class="form-style-flex" :model="formState" :label-col="{ style: { width: '80px' } }">
        <a-form-item name="zjm" label="">
          <a-input v-model:value="formState.zjm" class="w-64" placeholder="姓名/助记码/手机号" />
        </a-form-item>
        <a-form-item name="dateRange" label="购买日期">
          <a-range-picker v-model:value="formState.dateRange" class="w-64" format="YYYY-MM-DD" value-format="YYYY-MM-DD" />
        </a-form-item>
        <a-form-item name="mc" label=" ">
          <a-input v-model:value="formState.mc" class="w-64" placeholder="服务项目/助记码" />
        </a-form-item>
      </a-form>
    </SearchContainer>
    <a-card class="mt-2">
      <div class="flex mb-2">
        <BaseExport
          type="理疗"
          name="服务项目统计"
          code="000421"
          :search-params="{ startTime: formState.dateRange?.[0], endTime: formState.dateRange?.[1], ...formState }"
          >导出</BaseExport
        >
      </div>
      <BaseTable
        :data-source="tableData"
        fill
        :expanded-row-keys="expandedRowKeys"
        :loading="loading"
        column-code="000421"
        :pagination="pagination"
        @expand="onShowDetail"
      >
        <template #bodyCell="{ column, index, record }">
          <template v-if="column.dataIndex === 'NO'">
            {{ index + 1 }}
          </template>

          <template v-else-if="column.dataIndex === 'brName'">
            <PopoverTable :config="detailConfig" :params="{ brId: record.brId, itemCode: record.itemCode }" :width="800">
              <template #bodyCell="{ column: popColumn, index: indexChildren }">
                <template v-if="popColumn.dataIndex === 'NO'">
                  {{ indexChildren + 1 }}
                </template>
              </template>
              {{ record.brName }}
            </PopoverTable>
          </template>
        </template>
      </BaseTable>
    </a-card>
  </div>
</template>

<script setup>
import { onMounted, ref } from 'vue'
import dayjs from 'dayjs'
import SearchContainer from '@/components/SearchContainer'
// import dayjs from 'dayjs'
import BaseTable from '@/components/BaseTable'
// import { message } from 'ant-design-vue'

const formState = ref({
  dateRange: [dayjs().format('YYYY-MM-DD'), dayjs().format('YYYY-MM-DD')]
})

const detailConfig = ref({
  url: '/course/statisticalQuery/getCourseRecords',
  columnCode: '000422',
  rowKey: 'id',
  formatResult: (data) => {
    return data.map((item, index) => ({
      ...item,
      xh: index + 1
    }))
  }
})

const { loading, tableData, getData, pagination } = useTable({
  url: '/course/statisticalQuery/getBuyHistoryInfo',
  immediate: true,
  params: formState.value,
  formatParams: (params) => {
    return {
      ...params,
      startTime: params.dateRange?.[0],
      endTime: params.dateRange?.[1]
    }
  }
})

onMounted(() => {
  getData()
})
</script>
